<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules"  label-width="120px" class="form-container" :inline="true">
      <div class="createPost-main-container">
        <el-row>
          <el-col :span="12" >
            <el-form-item  label=" 用  户  名 :"   :required="true"  prop="name" >
              <el-input v-model="postForm.name"  :maxlength="50"   />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
            <el-form-item  label=" 密  码 : "   :required="true"  prop="password" >
              <el-input  v-model="postForm.password"  type="password"  :maxlength="50"   />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
            <el-form-item  label=" 再次输入密码 : "   :required="true"  prop="repassword" >
              <el-input  v-model="postForm.repassword" type="password"  :maxlength="50"   />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
            <el-form-item  label=" 邮 箱 : "  prop="email"  >
              <el-input v-model="postForm.email"  :maxlength="50" required />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="是否删除 " :required="true"  prop="is_del">

                <el-radio :label="0" v-model="postForm.is_del"  >正常</el-radio>
                <el-radio :label="1" v-model="postForm.is_del" >删除</el-radio>

            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" style="padding-left:60px;">
            <el-button type="primary" size="small" icon="el-icon-edit" @click="submitForm">提  交</el-button>
            <el-button  type="primary" size="small" icon="el-icon-lock" class="error" @click="resetForm">重  置</el-button>
          </el-col>
        </el-row>

      </div>
    </el-form>

  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import { validateURL } from '@/utils/validate'
import { adminCreate } from '@/api/admin'
import { userSearch } from '@/api/remoteSearch'
import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from '@/components/Dropdown'

const defaultForm = {
  name: '', //
  password: '', //
  repassword: '', //
  email: '', //
  role_id: '', //
  site_id: '',
  is_del: 0
}

export default {
  name: 'AdminAdd',
  components: { Tinymce, MDinput, Upload, Sticky,   CommentDropdown, PlatformDropdown, SourceUrlDropdown },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        // this.$message({
        //   message: rule.field + '为必传项!',
        //   type: 'error'
        // })
        if(rule.field == 'name'){
          callback(new Error('用户名为必传项！'))
        }
        if(rule.field == 'password'){
          callback(new Error('密码为必传项！'))
        }


      } else {
        callback()
      }
    }

    return {
      postForm: Object.assign({}, defaultForm),
      loading: false,
      userListOptions: [],
      is_del:0,
      rules: {
        name: [{ validator: validateRequire }],
        password: [{ validator: validateRequire }]
      }
    }
  },
  computed: {
    contentShortLength() {
      return this.postForm.content_short.length
    }
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      //this.fetchData(id)
    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
  },
  methods: {
    fetchData(id) {

    },
    resetForm() {
      this.$refs['postForm'].resetFields()
    },
    submitForm() {
      //this.postForm.display_time = parseInt(this.display_time / 1000)
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          if(this.postForm.password != this.postForm.repassword){
            this.$message({
              message:  '两次密码不一致！',
              type: 'error'
            })
            return ;
          }
          this.doSubmitForm()
          // this.$notify({
          //   title: '成功',
          //   message: '发布文章成功',
          //   type: 'success',
          //   duration: 2000
          // })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    doSubmitForm() {
      adminCreate(this.postForm).then(response => {
        if(response.data.code == 0){
          this.$message({
            message:  '保存成功!',
            type: 'success'
          })
          this.resetForm()
        }else{
          this.$message({
            message:  '保存失败!',
            type: 'error'
          })
        }
        // this.list = response.data.items
        // this.total = response.data.total
        this.listLoading = false
      })
    },
    getRemoteUserList(query) {
      userSearch(query).then(response => {
        if (!response.data.items) return
        this.userListOptions = response.data.items.map(v => v.name)
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/mixin.scss";
.createPost-container {
  position: relative;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
      .postInfo-container-item {
        float: left;
      }
    }
    .editor-container {
      min-height: 500px;
      margin: 0 0 30px;
      .editor-upload-btn-container {
        text-align: right;
        margin-right: 10px;
        .editor-upload-btn {
          display: inline-block;
        }
      }
    }
  }
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
</style>
